const app = getApp()
const ajax = app.myRequest()
import storage from "../../utils/storage.js"
Page({
    data: {
        list: [],
        user: null,
        parent: null
    },

    onLoad: function (options) {
        var that = this
        const scene = decodeURIComponent(options.scene)
        let parent = scene.split("&amp;")[0]
        that.setData({ parent: parent })

        // 获取个人信息
        ajax.getRequest('/User/profile').then((res) => {
            that.setData({ user: res.data.data })
            let parent_id = that.data.parent
            // 父级的邀请记录
            ajax.getRequest('/User/FriendHelpRecord', { parent_id }).then((res) => {
                if (res.data.code == 1) that.setData({ list: res.data.data })
            })
        })
    },

    // 授权手机号
    bindgetphonenumber: function (e) {
        if (e.detail.errMsg === "getPhoneNumber:ok") {
            let that = this
            let encryptedData = e.detail.encryptedData
            let iv = e.detail.iv
            ajax.getRequest('/User/authPhone', { encryptedData, iv }).then((res) => {
                storage.setStorage('mobile', res.data.data)
                // 获取个人信息
                ajax.getRequest('/User/profile').then((res) => {
                    that.setData({ user: res.data.data })
                    that.zhuli()
                })
            })
        }
    },

    // 授权昵称
    bindGetUserInfo: function (e) {
        let that = this
        // 检查是否授权
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: function (res) {
                            let nickname = res.userInfo.nickName
                            let avatar = res.userInfo.avatarUrl
                            ajax.getRequest('/User/auth', { nickname, avatar }).then((res) => {
                                wx.setStorageSync('is_guide', "1")
                                // 获取个人信息
                                ajax.getRequest('/User/profile').then((res) => {
                                    that.setData({ user: res.data.data })
                                    that.baoming()
                                })
                            })
                        }
                    })
                }
            }
        })
    },

    // 助力
    zhuli: function () {
        let that = this
        let parent_id = that.data.parent
        ajax.getRequest('/User/friendHelp', { parent: parent_id }).then((res) => {
            wx.showModal({
                showCancel: false,
                content: res.data.msg,
                title: '提示',
                success: function (e) {
                    if (e.confirm) {
                        if (res.data.code == 1) {
                            // 父级的邀请记录
                            ajax.getRequest('/User/FriendHelpRecord', { parent_id }).then((res) => {
                                if (res.data.code == 1) that.setData({ list: res.data.data })
                            })
                        }
                    }
                }
            })
        })
    },

    // 我要报名
    baoming: function () {
        wx.navigateTo({
            url: '/pages/activity/newUserActivity',
        })
    }
})